package bstsetcode.linkedliststack;

/**
 * @author noob
 * @version 1.0
 * @date 2021/3/14 11:11
 */
public class ArrayStack<E> implements Stack<E> {

    DynamicArrayT<E> array;

    public ArrayStack(int capacity) {
        array = new DynamicArrayT<>(capacity);
    }

    public ArrayStack() {
        array = new DynamicArrayT<>();
    }

    @Override
    public int getSize() {
        return array.getSize();
    }

    @Override
    public boolean isEmpty() {
        return array.isEmpty();
    }

    @Override
    public void push(E e) {
        array.addLast(e);
    }

    @Override
    public E pop() {
        return array.removeLast();
    }

    @Override
    public E peek() {
        return array.getElementByIndex(array.getSize()-1);
    }


    public int getCapacity(){
        return array.getCapacity();
    }


    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append("ArrayStack: ");
        res.append("[");
        for (int i = 0; i < array.getSize(); i++) {
            res.append(array.getElementByIndex(i));
            if(i != array.getSize() - 1){
                res.append(", ");
            }
        }
        res.append(" ] TOP");
        return res.toString();
    }
}
